#pragma once
#include <map>
#include <iostream>
#include <fstream>
#include <sstream>
#include <string>

using namespace std;

class translator
{
private:
	map<string, string> dict;
public:

	translator(){ }

	translator(string filename)
	{
		read_dict_from_file(filename);
	}

	~translator()
	{
	}

	void read_dict_from_file(string filename)
	{
		string str, row, word1, word2;
		try
		{
			ifstream fin(filename);
			while (getline(fin, row))
			{
				istringstream iss(row);
				getline(iss, str, '(');
				getline(iss, word1, ',');
				getline(iss, word2, ')');
				dict.emplace(word1, word2);
			}
			fin.close();
		}
		catch (exception e)
		{
			cout << e.what() << endl;
		}
	}

	void translate(string input_file, string output_file)
	{
		int success = 0;
		int fail = 0;
		string str, end;
		try
		{
			ifstream fin(input_file);
			ofstream fout(output_file);
			while (!fin.eof())
			{
				fin >> str;
				if (str.back() == *"\," || str.back() == *"\.")
				{
					end = str.back();
					end += ' ';
					str.erase(str.end() - 1);
				}
				else
					end = ' ';
				auto it = dict.find(str);
				if (it != dict.end())
				{
					fout << it->second << end;
					success++;
				}
				else
				{
					fout << str << end;
					fail++;
				}
			}
			fin.close();
			fout.close();
		}
		catch (exception e)
		{
			cout << e.what() << endl;
		}
		cout << success << " words successfully translated.." << endl;
		cout << fail << " words missing translation.." << endl;
	}
};

